package com.zueb.service.impl;

import com.zueb.mapper.UserMapper;
import com.zueb.pojo.User;
import com.zueb.pojo.UserInfo;
import com.zueb.service.UserService;
import com.zueb.util.PageUtils;
import com.zueb.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserServiceImpl implements UserService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public void addUser(User user) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        userMapper.addUser(user);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public User getUserByNumber(String number) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //调用方法
        User user = userMapper.getUserByNumber(number);

        //释放资源
        sqlSession.close();

        return user;
    }

    @Override
    public List<UserInfo> getAllUser(String userRole) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //调用方法
        List<UserInfo> users = userMapper.getAllUser(userRole);

        //释放资源
        sqlSession.close();

        return users;
    }

    @Override
    public int getCount(String userRole, int classesId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        int count = userMapper.getCount(userRole, classesId);
        //释放资源
        sqlSession.close();
        return count;
    }

    @Override
    public List<UserInfo> getAllStudent(String userRole, int facultyId, int classesId, int currentPage, int pageSize) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int index = pageSize * (currentPage - 1);
        //调用方法
        List<UserInfo> users = userMapper.getAllStudent(userRole, facultyId, classesId, index, pageSize);

        //释放资源
        sqlSession.close();

        return users;
    }

    @Override
    public List<UserInfo> getAllUserInfo(String userRole, int facultyId, int classesId, int currentPage, int pageSize) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int index = PageUtils.getIndex(currentPage, pageSize);
        //调用方法
        List<UserInfo> users = userMapper.getAllUserInfo(userRole, facultyId, classesId, index, pageSize);
        //释放资源
        sqlSession.close();

        return users;
    }

    @Override
    public int getAlUserInfoCount(String userRole, int facultyId, int classesId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        int count = userMapper.getAlUserInfoCount(userRole, facultyId, classesId);
        //释放资源
        sqlSession.close();
        return count;

    }

    @Override
    public int resetPass(String number) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        int resetPass = userMapper.resetPass(number);
        //提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return resetPass;
    }

    @Override
    public int delete(String number) {

        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //调用方法
        int deleteUser = userMapper.deleteUser(number);
        userMapper.setRegister(number);

        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return deleteUser;
    }

    @Override
    public int editPass(User user) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        //调用方法
        int flag = userMapper.editPass(user);
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return flag;
    }
}
